package com.bw.module_home.ui.marketers

import android.content.DialogInterface
import android.graphics.Color
import android.util.Log
import androidx.activity.viewModels
import androidx.appcompat.app.AlertDialog
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.bw.lib_common.base.BaseActivity
import com.bw.module_home.R
import com.bw.module_home.databinding.ActivityResidencyApplicationMarketersBinding
import com.bw.module_home.marketer.adapter.MarketersAdapter
import com.bw.module_home.marketer.viewmodel.MarketersViewModel
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = "/module_marketers/marketersActivity")
class ResidencyApplicationMarketersActivity : BaseActivity<MarketersViewModel, ActivityResidencyApplicationMarketersBinding>() {

    @Autowired
    @JvmField
    var marketId: Int = 0
    lateinit var marketersAdapter: MarketersAdapter
    var isClick: Boolean = true
    private val viewModel: MarketersViewModel by viewModels()

    override val mLayoutResId: Int = R.layout.activity_residency_application_marketers

    override fun initView() {
        super.initView()
        ARouter.getInstance().inject(this)
        marketersAdapter = MarketersAdapter()
        mBinding.rvSp.adapter = marketersAdapter
        mBinding.rvSp.layoutManager = LinearLayoutManager(this)
        mBinding.btnWait.setTextColor(Color.parseColor("#FFFFFF"))
        mBinding.btnWait.setBackgroundColor(Color.parseColor("#10B981"))

        viewModel.getResidencyApplicationList()
        mBinding.back.setOnClickListener {
            finish()
        }
        mBinding.btnWait.setOnClickListener {
            isClick = true
            mBinding.btnWait.setTextColor(Color.parseColor("#FFFFFF"))
            mBinding.btnWait.setBackgroundColor(Color.parseColor("#10B981"))
            mBinding.btnConfirm.setTextColor(Color.parseColor("#6B7280"))
            mBinding.btnConfirm.setBackgroundColor(Color.parseColor("#FFFFFF"))
            viewModel.getResidencyApplicationList()
        }
        mBinding.btnConfirm.setOnClickListener {
            isClick = false
            mBinding.btnConfirm.setTextColor(Color.parseColor("#FFFFFF"))
            mBinding.btnConfirm.setBackgroundColor(Color.parseColor("#10B981"))
            mBinding.btnWait.setTextColor(Color.parseColor("#6B7280"))
            mBinding.btnWait.setBackgroundColor(Color.parseColor("#FFFFFF"))
            viewModel.getResidencyApplicationList()
        }
        marketersAdapter.setOnItemClickListener { adapter, view, position ->
            AlertDialog.Builder(this)
                .setTitle("是否通过审核")
                .setPositiveButton("确定", object : DialogInterface.OnClickListener {
                    override fun onClick(p0: DialogInterface?, p1: Int) {
                        viewModel.approveResidencyApplication(marketId)
                    }
                })
                .setNegativeButton("驳回", object : DialogInterface.OnClickListener {
                    override fun onClick(p0: DialogInterface?, p1: Int) {
                        viewModel.rejectResidencyApplication(marketId)
                    }
                })
                .show()
        }
    }

    override fun initListener() {

    }

    override fun initData() {
        observeDataChange()
    }

    private fun observeDataChange() {
        viewModel.getResidencyApplicationListSuccess.observe(this) {
            val data = if (isClick) it.filter { it.status == "新建" || it.status == "待审核" }
            else it.filter { it.status == "已审核" || it.status == "驳回" }
            Log.d("marketerList", "${data}")
            marketersAdapter.data.clear()
            marketersAdapter.data.addAll(data)
            marketersAdapter.notifyDataSetChanged()
        }
        viewModel.approveResidencyApplicationSuccess.observe(this) {
            ToastUtils.showLong("已通过审核")
            viewModel.getResidencyApplicationList()
        }
        viewModel.rejectResidencyApplicationSuccess.observe(this) {
            ToastUtils.showLong("已驳回申请")
//            viewModel.getResidencyApplicationList()
        }
    }
}